package top.wshape1.shiziapi.project.domain;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
 * @TableName t_project_members
 */
@TableName(value = "t_project_members")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ProjectMembers implements Serializable {
    /**
     * 项目ID
     */
    @TableId
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    /**
     * 项目拥有者
     */
    private Long userId;

    /**
     * 项目成员们
     * userId：Member
     */
    private Object members;

    @TableField(exist = false)
    private Map<Long, Member> memberMap;

    public Map<Long, Member> getMemberMap() {
        if (memberMap == null) {
            memberMap = JSON.parseObject((String) members, new TypeReference<>() {
            });
        }
        return memberMap;
    }

    public Member getMember(Long userId) {
        return getMemberMap().get(userId);
    }

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}